package cn.wfu.rlrjds.web.user.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.wfu.rlrjds.app.bean.UserSearch;
import cn.wfu.rlrjds.app.log.domain.Log;
import cn.wfu.rlrjds.app.log.service.ILogService;
import cn.wfu.rlrjds.app.user.domain.User;
import cn.wfu.rlrjds.app.user.service.IUserService;
import cn.wfu.rlrjds.app.user.service.ipml.UserEcxeption;
import cn.wfu.rlrjds.container.ServiceProvinder;
import cn.wfu.rlrjds.frame.utils.Const;
import cn.wfu.rlrjds.utils.GsonUtils;
import cn.wfu.rlrjds.utils.IpUtil;
import cn.wfu.rlrjds.utils.UUIDUtil;

/**
 * 用户控制器
 * 
 * @author MGL
 * 
 */
@Controller(value = "webUserController")
@RequestMapping(value = "/web/user")
public class UserController {
	@Autowired
	private IUserService userService;

	/**
	 * 用户注册
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/registUser")
	@ResponseBody
	public String regist(HttpServletRequest request) {
		String userStr = request.getParameter("user");
		System.out.println(userStr);
		User form = GsonUtils.fromJson(userStr, User.class);
		String phone = form.getPhone();
		if (!phone.matches("^1[3|4|5|7|8]\\d{9}$")) {
			return Const.PHONE_EXIST;
		}
		try {
			userService.regist(form);
		} catch (UserEcxeption e) {
			// TODO Auto-generated catch block
			return Const.NAME_EXIST;
		}
		return Const.SUCCESS;
	}

	@RequestMapping(value = "/loginUser", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String login(HttpServletRequest request, HttpServletResponse response) {
		ILogService logService = (ILogService) ServiceProvinder
				.getService("logServiceImpl");
		try {
			String uid = request.getParameter("uid");
			String ip = IpUtil.getIpAddr(request);
			User user = userService.findUserById(uid);
			Log log = new Log();
			log.setIp(ip);
			log.setLogDate(new Date());
			log.setLogId(UUIDUtil.uuid());
			log.setUserName(user.getUid());
			log.setType(0);
			logService.saveLog(log);
			return GsonUtils.toJson(user);
		} catch (UserEcxeption e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 查找所有用户
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/list", produces = "text/html;charset=UTF-8")
	public String list(HttpServletRequest request, UserSearch userSearch) {
		String uid = userSearch.getUid();
		String name = userSearch.getName();
		String phone = userSearch.getPhone();

		if (StringUtils.isNotBlank(uid)) {
			userSearch.setUid("%" + uid.trim() + "%");
		}
		if (StringUtils.isNotBlank(name)) {
			userSearch.setName("%" + name.trim() + "%");
		}
		if (StringUtils.isNotBlank(phone)) {
			userSearch.setPhone("%" + phone.trim() + "%");
		}
		List<User> users = this.userService.findAllUser(userSearch);
		request.setAttribute("users", users);
		return "/user/list";

	}

	/**
	 * 保存用户
	 * 
	 * @param request
	 * @return
	 */

	@RequestMapping(value = "/save", produces = "text/html;charset=UTF-8")
	public String save(HttpServletRequest request, User user) {
		String uid = request.getParameter("id");
		String name = request.getParameter("nickName");
		user.setUid(uid);
		user.setName(name);
		try {
			this.userService.regist(user);
			return "redirect:/web" + this.list(request, new UserSearch());
		} catch (UserEcxeption e) {
			// TODO Auto-generated catch block
			request.setAttribute("msg", e.getMessage());
			request.setAttribute("user", user);
			return "/user/add";
		}
	}

	/**
	 * 删除用户
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/delete", produces = "text/html;charset=UTF-8")
	public String delete(HttpServletRequest request) {
		try {
			String[] ids = request.getParameterValues("ids");
			this.userService.deleteUsers(ids);
			return list(request, new UserSearch());
		} catch (UserEcxeption e) {
			// TODO Auto-generated catch block
			request.setAttribute("msg", e.getMessage());
			return "msg";
		}
	}

	/**
	 * 修改用户信息
	 * 
	 * @param request
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/updateInfo", produces = "text/html;charset=UTF-8")
	public String updateInfo(HttpServletRequest request, User user) {
		String uid = request.getParameter("id");
		String name = request.getParameter("nickName");
		user.setUid(uid);
		user.setName(name);
		try {
			this.userService.updateUserInfo(user, null);
			return list(request, new UserSearch());
		} catch (UserEcxeption e) {
			// TODO Auto-generated catch block
			request.setAttribute("msg", e.getMessage());
			return "msg";
		}
	}

	/**
	 * 修改用户密码
	 * 
	 * @param request
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/updatePassword", produces = "text/html;charset=UTF-8")
	public String updatePassword(HttpServletRequest request, User form) {
		String confirm_password = request.getParameter("confirm_password");
		if (!form.getPassword().equals(confirm_password)) {
			throw new RuntimeException("两次输入的密码不正确");
		}
		try {
			this.userService.updatePasswordNoValidate(form);
			return list(request, new UserSearch());
		} catch (UserEcxeption e) {
			// TODO Auto-generated catch block
			request.setAttribute("msg", e.getMessage());
			return "msg";
		}
	}

	/**
	 * 跳转到添加用户界面
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/add", produces = "text/html;charset=UTF-8")
	public String add(HttpServletRequest request) {
		return "/user/add";
	}

	/**
	 * 跳转到用户信息编辑页面
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/editUserInfo", produces = "text/html;charset=UTF-8")
	public String editUserInfo(HttpServletRequest request) {
		String uid = request.getParameter("uid");
		if (StringUtils.isBlank(uid)) {
			throw new RuntimeException("传递的参数名不能为空");
		}
		try {
			User user = this.userService.findUserById(uid);
			request.setAttribute("user", user);
			return "/user/edit_info";
		} catch (UserEcxeption e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			request.setAttribute("msg", "查找的用户不存在");
			return "msg";
		}
	}

	/**
	 * 跳转到密码修改页面
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/editPassword", produces = "text/html;charset=UTF-8")
	public String editPassword(HttpServletRequest request) {
		request.setAttribute("uid", request.getParameter("uid"));
		return "/user/edit_password";
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

}
